package com.cwz.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wenZhen
 * @create 2024-08-13 14:06
 */


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //系统异常
    @ExceptionHandler(Exception.class)
    public Map<String,Object> handlerException(Exception e){
        log.error("发生异常:{}",e);
        log.error("发生异常:{}",e.getMessage());//打印异常日志给我看

        //返回一个map给前端
        Map<String,Object> map=new HashMap<>();
        map.put("code",ErrorEnum.SERVER_ERROR.getCode());
        map.put("msg",ErrorEnum.SERVER_ERROR.getMsg());
        return map;
    }


    //处理业务异常
    @ExceptionHandler(ServiceException.class)
    public Map<String,Object> handleServiceException(ServiceException e){
        log.error("发生异常:{}",e);
        log.error("发生异常:{}",e.getMessage());//打印异常日志给我看

        //返回一个map给前端
        Map<String,Object> map=new HashMap<>();
        map.put("code",e.getErrorEnum().getCode());
        map.put("msg",e.getErrorEnum().getMsg());
        return map;
    }

}
